<?php
require_once 'init_app.php';

set_time_limit(0); 

$base_category_id = $category_id = $brand_id = $color_id = $composition_id =
$season_id = $shop_id = $count = 0;
$price_from = $price_to = null;
$big_category_ids = ORM::factory('category')->get_big_categories();

// Для крупных категорий считать все
/*$base_categories = ORM::factory('category')->where('id', 'IN', $big_category_ids)->find_all();
foreach ($base_categories as $base_category) {
    
    $base_category = $base_category->as_array();
    $base_category_id = $base_category['id'];

    //$categories = ORM::factory('category')->get_categories($base_category, null, null, null, null, null, null, false);
    $categories[] = array(
        'id' => 0,
        'childs' => null,
    );
    foreach ($categories as $category) {
    
        $category_id = $category['id'];
        
        $brands = ORM::factory('brand')->get_brands($category); 
        $brands[] = array(
            'id' => 0,
        ); 
        foreach ($brands as $brand) {
            
            $brand_id = $brand['id'];
            
            $colors  = Model_Color::get_colors($category, $brand);
            $colors[] = array(
                'id' => 0,
            ); 
            foreach ($colors as $color) {
                
                $color_id = $color['id'];
                
                $compositions = Model_Composition::get_compositions($category, $brand, $color);
                $compositions[] = array(
                    'id' => 0,
                ); 
                foreach ($compositions as $composition) {
                    
                    $composition_id = $composition['id'];
                    
                    $seasons = Model_Season::get_seasons($category, $brand, $color, $composition);
                    $seasons[] = array(
                        'id' => 0,
                    ); 
                    foreach ($seasons as $season) {
                        
                        $season_id = $season['id'];
                        
                        $shops = ORM::factory('shop')->get_shops($category, $brand, $color, $composition, $season);
                        $shops[] = array(
                            'id' => 0,
                        ); 
                        foreach ($shops as $shop) {
                            
                            $shop_id = $shop['id'];
                            
                            $prices = Model_Product::get_prices($category, $brand, $color, $composition, $season, $shop);
                            $prices[] = array(
                                'from' => 0,
                                'to'   => 0,
                            ); 
                            foreach ($prices as $price) {
                                
                                $price_from = $price['from'];
                                $price_to = $price['to'];
                                
                                $current_category = $base_category;
                                if ($category['id']) {
                                    $current_category = $category['id'];    
                                } 
                                $count = ORM::factory('product')->get_products($current_category, $brand_id, $color_id, $composition_id, $season_id, $shop_id, array('from' => $price_from, 'to' => $price_to), null, true);
                                
                                set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
                            }  
                            $price_from = $price_to = 0; 
                        }
                        $shop_id = 0;
                    }
                    $season_id = 0;                     
                }
                $composition_id = 0;              
            }
            $color_id = 0;   
        } 
        $brand_id = 0; 
    }
    $category_id = 0;  
}
$base_category_id = 0;*/ 

// Для остальных - только без фильтров и 1 фильтр
$base_categories = ORM::factory('category')/*->where('id', 'NOT IN', $big_category_ids)*/->find_all();
foreach ($base_categories as $base_category) {
    
    $base_category_id = $base_category->id;
    $count = ORM::factory('product')->get_products($base_category, null, null, null, null, null, null, null, true);
    
    set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
    
    $categories = ORM::factory('category')->get_categories($base_category, null, null, null, null, null, null, false, false, true);
    foreach ($categories as $category) {
    
        $category_id = $category['id'];
        $count = $category['count'];
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                  
    
        $brands = ORM::factory('brand')->get_brands($category, null, null, null, null, null, null, false, true);  
        foreach ($brands as $brand) {
            
            $brand_id = $brand['id'];
            $count = $brand['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        } 
        $brand_id = 0;
        
        $colors  = Model_Color::get_colors($category, null, null, null, null, null, null, false, true);
        foreach ($colors as $color) {
            
            $color_id = $color['id'];
            $count = $color['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);            
        }
        $color_id = 0;
        
        $compositions = Model_Composition::get_compositions($category, null, null, null, null, null, null, false, true);
        foreach ($compositions as $composition) {
            
            $composition_id = $composition['id'];
            $count = $composition['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);    
        }
        $composition_id = 0; 
        
        $seasons = Model_Season::get_seasons($category, null, null, null, null, null, null, false, true);
        foreach ($seasons as $season) {
            
            $season_id = $season['id'];
            $count = $season['count']; 
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                 
        }
        $season_id = 0;  
        
        $shops = ORM::factory('shop')->get_shops($category, null, null, null, null, null, null, false, true);
        foreach ($shops as $shop) {
            
            $shop_id = $shop['id'];
            $count = $shop['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        }
        $shop_id = 0;
        
        $prices = Model_Product::get_prices($category, null, null, null, null, null, null, false, true);
        foreach ($prices as $price) {
            
            $price_from = $price['from'];
            $price_to = $price['to']; 
            $count = $price['count'];  
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
        }  
        $price_from = $price_to = null;    
    }
    $category_id = 0;    
    
    /*$brands = ORM::factory('brand')->get_brands($base_category, null, null, null, null, null, null, false, true);  
    foreach ($brands as $brand) {
        
        $brand_id = $brand['id'];
        $count = $brand['count'];
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
    
        $categories = ORM::factory('category')->get_categories($base_category, $brand, null, null, null, null, null, false, false, true);
        foreach ($categories as $category) {
        
            $category_id = $category['id'];
            $count = $category['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                  
        }
        $category_id = 0;
        
        $colors  = Model_Color::get_colors($base_category, $brand, null, null, null, null, null, false, true);
        foreach ($colors as $color) {
            
            $color_id = $color['id'];
            $count = $color['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);            
        }
        $color_id = 0;
        
        $compositions = Model_Composition::get_compositions($base_category, $brand, null, null, null, null, null, false, true);
        foreach ($compositions as $composition) {
            
            $composition_id = $composition['id'];
            $count = $composition['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);    
        }
        $composition_id = 0; 
        
        $seasons = Model_Season::get_seasons($base_category, $brand, null, null, null, null, null, false, true);
        foreach ($seasons as $season) {
            
            $season_id = $season['id'];
            $count = $season['count']; 
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                 
        }
        $season_id = 0;  
        
        $shops = ORM::factory('shop')->get_shops($base_category, $brand, null, null, null, null, null, false, true);
        foreach ($shops as $shop) {
            
            $shop_id = $shop['id'];
            $count = $shop['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        }
        $shop_id = 0;
        
        $prices = Model_Product::get_prices($base_category, $brand, null, null, null, null, null, false, true);
        foreach ($prices as $price) {
            
            $price_from = $price['from'];
            $price_to = $price['to']; 
            $count = $price['count'];  
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
        }  
        $price_from = $price_to = null;
    } 
    $brand_id = 0; 
    
    $colors  = Model_Color::get_colors($base_category, null, null, null, null, null, null, false, true);
    foreach ($colors as $color) {
        
        $color_id = $color['id'];
        $count = $color['count'];
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);            
    
        $categories = ORM::factory('category')->get_categories($base_category, null, $color, null, null, null, null, false, false, true);
        foreach ($categories as $category) {
        
            $category_id = $category['id'];
            $count = $category['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                  
        }
        $category_id = 0;
        
        $brands = ORM::factory('brand')->get_brands($base_category, null, $color, null, null, null, null, false, true);  
        foreach ($brands as $brand) {
            
            $brand_id = $brand['id'];
            $count = $brand['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        } 
        $brand_id = 0;
        
        $compositions = Model_Composition::get_compositions($base_category, null, $color, null, null, null, null, false, true);
        foreach ($compositions as $composition) {
            
            $composition_id = $composition['id'];
            $count = $composition['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);    
        }
        $composition_id = 0; 
        
        $seasons = Model_Season::get_seasons($base_category, null, $color, null, null, null, null, false, true);
        foreach ($seasons as $season) {
            
            $season_id = $season['id'];
            $count = $season['count']; 
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                 
        }
        $season_id = 0;  
        
        $shops = ORM::factory('shop')->get_shops($base_category, null, $color, null, null, null, null, false, true);
        foreach ($shops as $shop) {
            
            $shop_id = $shop['id'];
            $count = $shop['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        }
        $shop_id = 0;
        
        $prices = Model_Product::get_prices($base_category, null, $color, null, null, null, null, false, true);
        foreach ($prices as $price) {
            
            $price_from = $price['from'];
            $price_to = $price['to']; 
            $count = $price['count'];  
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
        }  
        $price_from = $price_to = null;
    }
    $color_id = 0;
    
    $compositions = Model_Composition::get_compositions($base_category, null, null, null, null, null, null, false, true);
    foreach ($compositions as $composition) {
        
        $composition_id = $composition['id'];
        $count = $composition['count'];
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);    
    
        $categories = ORM::factory('category')->get_categories($base_category, null, null, $composition, null, null, null, false, false, true);
        foreach ($categories as $category) {
        
            $category_id = $category['id'];
            $count = $category['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                  
        }
        $category_id = 0;
        
        $brands = ORM::factory('brand')->get_brands($base_category, null, null, $composition, null, null, null, false, true);  
        foreach ($brands as $brand) {
            
            $brand_id = $brand['id'];
            $count = $brand['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        } 
        $brand_id = 0;
        
        $colors  = Model_Color::get_colors($base_category, null, null, $composition, null, null, null, false, true);
        foreach ($colors as $color) {
            
            $color_id = $color['id'];
            $count = $color['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);            
        }
        $color_id = 0; 
        
        $seasons = Model_Season::get_seasons($base_category, null, null, $composition, null, null, null, false, true);
        foreach ($seasons as $season) {
            
            $season_id = $season['id'];
            $count = $season['count']; 
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                 
        }
        $season_id = 0;  
        
        $shops = ORM::factory('shop')->get_shops($base_category, null, null, $composition, null, null, null, false, true);
        foreach ($shops as $shop) {
            
            $shop_id = $shop['id'];
            $count = $shop['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        }
        $shop_id = 0;
        
        $prices = Model_Product::get_prices($base_category, null, null, $composition, null, null, null, false, true);
        foreach ($prices as $price) {
            
            $price_from = $price['from'];
            $price_to = $price['to']; 
            $count = $price['count'];  
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
        }  
        $price_from = $price_to = null;
    }
    $composition_id = 0;  
    
    $seasons = Model_Season::get_seasons($base_category, null, null, null, null, null, null, false, true);
    foreach ($seasons as $season) {
        
        $season_id = $season['id'];
        $count = $season['count']; 
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                 
    
        $categories = ORM::factory('category')->get_categories($base_category, null, null, null, $season, null, null, false, false, true);
        foreach ($categories as $category) {
        
            $category_id = $category['id'];
            $count = $category['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                  
        }
        $category_id = 0;
        
        $brands = ORM::factory('brand')->get_brands($base_category, null, null, null, $season, null, null, false, true);  
        foreach ($brands as $brand) {
            
            $brand_id = $brand['id'];
            $count = $brand['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        } 
        $brand_id = 0;
        
        $colors  = Model_Color::get_colors($base_category, null, null, null, $season, null, null, false, true);
        foreach ($colors as $color) {
            
            $color_id = $color['id'];
            $count = $color['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);            
        }
        $color_id = 0;
        
        $compositions = Model_Composition::get_compositions($base_category, null, null, null, $season, null, null, false, true);
        foreach ($compositions as $composition) {
            
            $composition_id = $composition['id'];
            $count = $composition['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);    
        }
        $composition_id = 0; 
        
        $shops = ORM::factory('shop')->get_shops($base_category, null, null, null, $season, null, null, false, true);
        foreach ($shops as $shop) {
            
            $shop_id = $shop['id'];
            $count = $shop['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        }
        $shop_id = 0;
        
        $prices = Model_Product::get_prices($base_category, null, null, null, $season, null, null, false, true);
        foreach ($prices as $price) {
            
            $price_from = $price['from'];
            $price_to = $price['to']; 
            $count = $price['count'];  
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
        }  
        $price_from = $price_to = null;
    }
    $season_id = 0;  
    
    $shops = ORM::factory('shop')->get_shops($base_category, null, null, null, null, null, null, false, true);
    foreach ($shops as $shop) {
        
        $shop_id = $shop['id'];
        $count = $shop['count'];
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
    
        $categories = ORM::factory('category')->get_categories($base_category, null, null, null, null, $shop, null, false, false, true);
        foreach ($categories as $category) {
        
            $category_id = $category['id'];
            $count = $category['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                  
        }
        $category_id = 0;
        
        $brands = ORM::factory('brand')->get_brands($base_category, null, null, null, null, $shop, null, false, true);  
        foreach ($brands as $brand) {
            
            $brand_id = $brand['id'];
            $count = $brand['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        } 
        $brand_id = 0;
        
        $colors  = Model_Color::get_colors($base_category, null, null, null, null, $shop, null, false, true);
        foreach ($colors as $color) {
            
            $color_id = $color['id'];
            $count = $color['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);            
        }
        $color_id = 0;
        
        $compositions = Model_Composition::get_compositions($base_category, null, null, null, null, $shop, null, false, true);
        foreach ($compositions as $composition) {
            
            $composition_id = $composition['id'];
            $count = $composition['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);    
        }
        $composition_id = 0; 
        
        $seasons = Model_Season::get_seasons($base_category, null, null, null, null, $shop, null, false, true);
        foreach ($seasons as $season) {
            
            $season_id = $season['id'];
            $count = $season['count']; 
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                 
        }
        $season_id = 0;  
        
        $prices = Model_Product::get_prices($base_category, null, null, null, null, $shop, null, false, true);
        foreach ($prices as $price) {
            
            $price_from = $price['from'];
            $price_to = $price['to']; 
            $count = $price['count'];  
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
        }  
        $price_from = $price_to = null;
    }
    $shop_id = 0;
    
    $prices = Model_Product::get_prices($base_category, null, null, null, null, null, null, false, true);
    foreach ($prices as $price) {
        
        $price_from = $price['from'];
        $price_to = $price['to']; 
        $count = $price['count'];  
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
    
        $categories = ORM::factory('category')->get_categories($base_category, null, null, null, null, null, $price, false, false, true);
        foreach ($categories as $category) {
        
            $category_id = $category['id'];
            $count = $category['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                  
        }
        $category_id = 0;
        
        $brands = ORM::factory('brand')->get_brands($base_category, null, null, null, null, null, $price, false, true);  
        foreach ($brands as $brand) {
            
            $brand_id = $brand['id'];
            $count = $brand['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        } 
        $brand_id = 0;
        
        $colors  = Model_Color::get_colors($base_category, null, null, null, null, null, $price, false, true);
        foreach ($colors as $color) {
            
            $color_id = $color['id'];
            $count = $color['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);            
        }
        $color_id = 0;
        
        $compositions = Model_Composition::get_compositions($base_category, null, null, null, null, null, $price, false, true);
        foreach ($compositions as $composition) {
            
            $composition_id = $composition['id'];
            $count = $composition['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);    
        }
        $composition_id = 0; 
        
        $seasons = Model_Season::get_seasons($base_category, null, null, null, null, null, $price, false, true);
        foreach ($seasons as $season) {
            
            $season_id = $season['id'];
            $count = $season['count']; 
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                 
        }
        $season_id = 0;  
        
        $shops = ORM::factory('shop')->get_shops($base_category, null, null, null, null, null, $price, false, true);
        foreach ($shops as $shop) {
            
            $shop_id = $shop['id'];
            $count = $shop['count'];
            
            set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
        }
        $shop_id = 0;
    }  
    $price_from = $price_to = null;*/   
}
$base_category_id = 0;   

// Для брендов - без фильтров и 1 фильтр без категории
/*$brands = DB::query(Database::SELECT, "SELECT *, (SELECT COUNT(*) FROM products WHERE brand_id = brands.id AND is_available = 1) AS count FROM brands
                                       ORDER BY count DESC
                                       LIMIT 1000")->execute()->as_array();  
foreach ($brands as $brand) {
    
    $brand_id = $brand['id'];
    $count = $brand['count'];
    
    set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 

    $colors  = Model_Color::get_colors(null, $brand);
    foreach ($colors as $color) {
        
        $color_id = $color['id'];
        $count = $color['count'];
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);            
    }
    $color_id = 0;

    $compositions = Model_Composition::get_compositions(null, $brand);
    foreach ($compositions as $composition) {
        
        $composition_id = $composition['id'];
        $count = $composition['count'];
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);    
    }
    $composition_id = 0; 

    $seasons = Model_Season::get_seasons(null, $brand);
    foreach ($seasons as $season) {
        
        $season_id = $season['id'];
        $count = $season['count']; 
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);                 
    }
    $season_id = 0;  

    $shops = ORM::factory('shop')->get_shops(null, $brand);
    foreach ($shops as $shop) {
        
        $shop_id = $shop['id'];
        $count = $shop['count'];
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count); 
    }
    $shop_id = 0;

    $prices = Model_Product::get_prices(null, $brand);
    foreach ($prices as $price) {
        
        $price_from = $price['from'];
        $price_to = $price['to']; 
        $count = $price['count'];  
        
        set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count);
    }  
    $price_from = $price_to = 0;
} 
$brand_id = 0;       */
                     

function set_denormalized_value($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price_from, $price_to, $shop_id, $count) {
    $price = '';
    if ($price_from || $price_to) {
        $price = $price_from.'_'.$price_to;   
    }
    try {
        DB::insert('catalog_filter_counts', array('base_category_id', 'category_id', 'brand_id', 'color_id', 'composition_id', 'season_id', 'price', 'shop_id', 'count'))
            ->values(array($base_category_id, $category_id, $brand_id, $color_id, $composition_id, $season_id, $price, $shop_id, $count))->execute();    
    } catch (Database_Exception $e) {
        DB::update('catalog_filter_counts')
            ->set(array('count' => $count))
            ->where('base_category_id', '=', $base_category_id)
            ->where('category_id', '=', $category_id) 
            ->where('brand_id', '=', $brand_id) 
            ->where('color_id', '=', $color_id) 
            ->where('composition_id', '=', $composition_id) 
            ->where('season_id', '=', $season_id) 
            ->where('price', '=', $price)  
            ->where('shop_id', '=', $shop_id) 
            ->execute();
    }    
}

echo 'ok';







